#include "sessiondetailwdiget.h"

////////////////////////////////////////
/// 会话详情
////////////////////////////////////////

SessionDetailWdiget::SessionDetailWdiget(const QString& userId, QWidget* parent)
    :QDialog(parent)
{
    // 设置必要的属性
    this->setFixedSize(QSize(400, 350));
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QWidget { background-color: rgb(200, 200, 200)}");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(50, 50, 0, 0);
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    this->setLayout(layout);

    AvatarItem* addItem = new AvatarItem(QIcon(":/resource/image/add_friend.png"), "添加");

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* userInfo = dataCenter->findUserInfoById(userId);
    AvatarItem* userItem = new AvatarItem(userInfo->avatar, userInfo->nickname);

    // 删除好友按钮
    QPushButton* deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(QSize(175, 50));
    deleteFriendBtn->setStyleSheet("QPushButton { font-size: 20px; color:rgb(240, 240, 240); "
                                   "border:1px solid; }"
                                   "QPushButton:pressed { background-color:rgb(100, 100, 100); }");

    layout->addWidget(addItem, 0, 0);
    layout->addWidget(userItem, 0, 1);
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 2);

    // click add button, pop up add dialog box
    connect(addItem->getAvatarBtn(), &QPushButton::clicked, this, [=](){
        ChooseFriendDialog* chooseFriendWidget = new ChooseFriendDialog(this, userId);
        chooseFriendWidget->exec();
    });

    // click delete button, delete current friend
    connect(deleteFriendBtn, &QPushButton::clicked, this, [=](){
        auto ret = QMessageBox::warning(nullptr, "delete friend", "delete friend?", QMessageBox::Ok | QMessageBox::Cancel);
        if(ret == QMessageBox::Cancel) {
            return;
        }
        dataCenter->deleteFriendAsync(userId);
        this->close();
    });
}

////////////////////////////////////////
/// 头像 + 昵称 Item
////////////////////////////////////////

AvatarItem::AvatarItem(const QIcon& avatar, const QString& name)
{
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    this->setLayout(layout);

    QString btnStyle = "QPushButton:pressed { background-color:rgb(100, 100, 100); }";
    btnStyle += "QPushButton { border:none; }";
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(50, 50));
    avatarBtn->setIcon(avatar);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setStyleSheet(btnStyle);

    QFont font("微软雅黑", 12);
    nameLabel = new QLabel();
    nameLabel->setFont(font);

    // 对 name 进行截断
    QFontMetrics metrics(font);
    int maxWidth = 65;
    int usedWidth = maxWidth - metrics.horizontalAdvance("...");
    int curWidth = 0;
    QString newName;
    bool flag = false;
    for(int i = 0;i < name.size();i ++) {
        curWidth += metrics.horizontalAdvance(name[i]);
        if(curWidth > usedWidth) {
            flag = true;
            break;
        }
        newName += name[i];
    }
    if(flag) {
        newName += "...";
        nameLabel->setText(newName);
    }
    else {
        nameLabel->setText(name);
    }

    layout->addWidget(avatarBtn, 0, Qt::AlignTop);
    layout->addWidget(nameLabel, 0, Qt::AlignTop | Qt::AlignLeft);
}

QPushButton *AvatarItem::getAvatarBtn()
{
    return avatarBtn;
}



















